package com.aerozhonghuan.logisticsmall.modules.order;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;

import com.aerozhonghuan.logisticsmall.R;
import com.aerozhonghuan.logisticsmall.framework.base.Constants;
import com.aerozhonghuan.logisticsmall.framework.base.TitlebarActivity;
import com.aerozhonghuan.logisticsmall.framework.base.TitlebarFragment;
import com.aerozhonghuan.logisticsmall.modules.order.adapter.MyOrdersViewPagerAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * @author zhangyonghui
 * @desc 订单结算
 */
public class OrderSettlementAcrivity extends TitlebarActivity {

    public static final String KEY_POSITION = "KEY_POSITION";
    private FrameLayout container;
    private LayoutInflater inflater;
    private View view;
    private TabLayout tablayout;
    private ViewPager viewpager;
    private List<TitlebarFragment> orderFragments;
    private MyOrdersViewPagerAdapter myOrdersViewPagerAdapter;
    private String[] tabNames = new String[]{"全部", "未结算", "已结算"};
    private int[] tabStatus = new int[]{-1, Constants.TYPE_REQUESTNOSETTLEMENT, Constants.TYPE_REQUESTSETTLEMENTED};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (savedInstanceState == null) {
            container = (FrameLayout) findViewById(R.id.container1);
            inflater = LayoutInflater.from(getContext());
            view = inflater.inflate(R.layout.activity_myorders, null);
            container.addView(view);
            initView();
        }
    }

    private void initView() {
        viewpager = (ViewPager) view.findViewById(R.id.viewpager);
        // 初始化设置tablayout
        tablayout = (TabLayout) view.findViewById(R.id.tablayout);
        orderFragments = new ArrayList<>();
        for (int i = 0; i < tabNames.length; i++) {
            tablayout.addTab(tablayout.newTab());
            Bundle bundle = new Bundle();
            bundle.putInt(OrderSettlementFragment.KEY_STATUS, tabStatus[i]);
            OrderSettlementFragment myOrdersFragment = new OrderSettlementFragment();
            myOrdersFragment.setArguments(bundle);
            orderFragments.add(myOrdersFragment);
            tablayout.getTabAt(i).setText(tabNames[i]);
        }
        myOrdersViewPagerAdapter = new MyOrdersViewPagerAdapter(this, orderFragments, tabNames);
        viewpager.setAdapter(myOrdersViewPagerAdapter);
        tablayout.setupWithViewPager(viewpager);
    }
}
